package vek.GUI.SearchAndListView;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import vek.controllers.SearchAndList.RightPanelViewController;

/**
 * This class is reponsible for showing the POIs which have been searched for by the user in the right side of the screen.
 * 
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class RightPanelView extends JPanel{
    
    RightPanelViewController controller; // the controller for this panel, handles user input
    private JPanel listPanel; // the panel that shows the POIs
    private JScrollPane scrollPaneForListPanel;
    private JButton hidePanelButton; // button that hides this panel
    private JPanel westPanel; // the pane
    
    /**
     * Creates the RightPanelView that will show POIs which have been searched for by the user in the right side of the screen.
     * 
     * @param screensize the screen size of the terminal
     */
    
    public RightPanelView(Dimension screensize)
    {
        
        setPreferredSize(new Dimension((int)(screensize.getWidth() * 0.3), (int)screensize.getHeight())); // sets the width to 30% of the screen width
        setVisible(false); // defaults to hidden
        listPanel = new JPanel();
        scrollPaneForListPanel = new JScrollPane();
        listPanel.setVisible(true);
        hidePanelButton = new JButton(">");
        hidePanelButton.setPreferredSize(new Dimension(40,20));
        hidePanelButton.setMaximumSize(new Dimension(40,20));
        hidePanelButton.setFont(new Font("Serif", Font.BOLD, 10));
        hidePanelButton.setName("hidepanel");
        
        westPanel = new JPanel();
        westPanel.setLayout(new BorderLayout());
        westPanel.add(hidePanelButton, BorderLayout.CENTER);
        westPanel.setMaximumSize(new Dimension((int)(screensize.getWidth() * 0.1), (int)screensize.getHeight()));
        
        
        //setLayout(new BorderLayout());
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
        
        addContent();
        
    }
    
    /**
     * Adds components to this component, that is makes them visible in this component.
     * The westpanel and the listpanel is added
     */
    
    private void addContent()
    {
        //add(westPanel, BorderLayout.WEST);
        //add(scrollPaneForListPanel, BorderLayout.EAST);
        add(westPanel);
        add(scrollPaneForListPanel);
        scrollPaneForListPanel.getViewport().add(listPanel);
        
    }
    
    /**
     * Sets the controller which this panel uses.
     * @param rpc the RightPanelViewController which this panel uses for user input
     */
    
    public void setController(RightPanelViewController rpc)
    {
        controller = rpc;
        hidePanelButton.addActionListener(controller);
    }
    
    /**
     * Shows the POIs the user has seached for as JButtons in a list
     * @param listOfPOIs the POIs which are to be shown as strings
     */
    
    public void showPOIs(ArrayList<String> listOfPOIs)
    {
        assert controller != null : "RightPanelView: controller == null";
        
        clearAllPOIs(); // removes all previous JButtons from the listpanel
        
        for(int i = 0; i < listOfPOIs.size(); i++)
        {
            JButton button = new JButton(listOfPOIs.get(i)); // create a Jbutton with the next POI as the text
            button.setName("poilistbutton:" + i);
            
            if(listOfPOIs.size() == 1 && listOfPOIs.get(i).toLowerCase().startsWith("ingen"))
            {
                // don't add action listener, becuase it has no snap to function
            }
            else
            {
              button.addActionListener(controller);   
            }
            listPanel.add( button );
        }
        
    }
    
    /**
     * removes all the POIs from the listpanel
     */
    public void clearAllPOIs()
    {
        listPanel.removeAll();
        listPanel.revalidate();
        listPanel.repaint();
    }
    
    /**
     * Hides or shows this panel
     */
    
    public void toggleVisible()
    {
        if(isVisible())
        {
            setVisible(false);
        }
        else
        {
            setVisible(true);
        }
    }

}
